/*
 * @(#)TestType.java
 *
 *
 *
 * ver 1.0 Aug 16, 2007 ggranum
 */
package org.testfire.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.testfire.util.DomUtil;
import org.testfire.translator.TranslatorFactory;
import org.w3c.dom.Element;

public class TestType implements TestFireObject
{
    TestFireObject parent;

    private final Element element;


    public TestType( Element element )
    {
        this.element = element;
    }

    public Object translate( TestsetType testset )
    {
        return TranslatorFactory.translate(testset, this);
    }

    public void setParent( TestFireObject parent )
    {
        this.parent = parent;
    }

    public TestFireObject getParent()
    {
        return parent;
    }


    public String getName()
    {
        return element.getAttribute( "name" );
    }

    public void setName(String name){
        element.setAttribute( "name", name);
    }


    public String getForBug()
    {
        return element.getAttribute( "forBug" );
    }

     public void setforBug(String forBug){
        element.setAttribute( "forBug", forBug);
    }

    public Date getCreated()
    {
        return DomUtil.toJavaDate( element.getAttribute( "created" ) );
    }

     public void setCreated(Date created){
        element.setAttribute( "created", DomUtil.toXMLDateTime( created ));
    }

    public String getDescription()
    {
        return DomUtil.getChildElementTextByTagName( "tf:description", element );
    }

    public void setDescription(String description)
    {
       Element el = DomUtil.getChildElementByTagName( "tf:description", element );
        if ( el == null )
        {
            el = element.getOwnerDocument().createElement( "tf:description" );
            element.insertBefore( el, element.getFirstChild());
        }
        el.setTextContent( description );
    }

    public List<TestResultType> getTestResults()
    {
        List<Element> children = DomUtil.getChildElementsByTagName( "tf:TestResults", element );
        List<TestResultType> testResults = new ArrayList<TestResultType>();

        for ( Element child : children )
        {
            testResults.add( new TestResultType( this, child ) );
        }
        return testResults;
    }

    public List<TasksType> getTasks()
    {
        List<Element> children = DomUtil.getChildElementsByTagName( "tf:tasks", element );
        List<TasksType> tasks = new ArrayList<TasksType>();

        for ( Element child : children )
        {
            tasks.add( new TasksType( child ) );
        }
        return tasks;
    }

    public void addTestResult(Element testResult){
        element.appendChild( testResult );
    }

    public TestResultType getTestResult(Date date){
        List<TestResultType> results = this.getTestResults();
        for(TestResultType r : results){
            if(r.getRunDate().getTime() == date.getTime()){
                return r;
            }
        }
        return null;
    }

    public Element getElement()
    {
        return element;
    }
}

// eof: TestType.java